<?php
/*
Plugin Name: SilverlightStreaming Widget
Plugin URI: http://www.codeplex.com/WordpressSLPlugins
Description: Insert Silverlight Streaming application with public SilverlightSteamingID and SilverlightStreamingApplicationName
Author: Pierre Lagarde Microsoft
Version: 1.0
License: Shared Source
Author URI: http://blogs.developpeur.org/fox
*/


function widget_silverlightStreaming_init() {
                

	if ( !function_exists('register_sidebar_widget') || !function_exists('register_widget_control') )
		return;

	function widget_silverlightStreaming_control($number) {
		$options = $newoptions = get_option('widget_silverlightStreaming');
		if ( $_POST["silverlightStreaming-submit-$number"] ) {
			$newoptions[$number]['title'] = strip_tags(stripslashes($_POST["silverlightStreaming-title-$number"]));
			$newoptions[$number]['width'] = strip_tags(stripslashes($_POST["silverlightStreaming-width-$number"]));
			$newoptions[$number]['height'] = strip_tags(stripslashes($_POST["silverlightStreaming-height-$number"]));
			$newoptions[$number]['idSLS'] = strip_tags(stripslashes($_POST["silverlightStreaming-idSLS-$number"]));
			$newoptions[$number]['appName'] = strip_tags(stripslashes($_POST["silverlightStreaming-appName-$number"]));
		}
   		if ($options[$number]['count']=='') {
 			$newoptions[$number]['count'] = 4;
		}				
		if ( $options != $newoptions ) {
			$options = $newoptions;
			update_option('widget_silverlightStreaming', $options);
		}
		    
	?>
		<label for="silverlightStreaming-title-<?php echo "$number"; ?>" title="Title above the widget" style="line-height:35px;display:block;">Title: <input type="text" style="width: 442px;" id="silverlightStreaming-title-<?php echo "$number"; ?>" name="silverlightStreaming-title-<?php echo "$number"; ?>" value="<?php echo htmlspecialchars($options[$number]['title']); ?>" /></label><br />
		<input type="hidden" name="silverlightStreaming-submit-<?php echo "$number"; ?>" id="silverlightStreaming-submit-<?php echo "$number"; ?>" value="1" />
		<label for="silverlightStreaming-width-<?php echo "$number"; ?>" title="Width" style="line-height:35px;">Width: <input type="integer" style="width: 80px;" id="silverlightStreaming-width-<?php echo "$number"; ?>" name="silverlightStreaming-width-<?php echo "$number"; ?>" value="<?php echo htmlspecialchars($options[$number]['width']); ?>" /></label><br />
		<label for="silverlightStreaming-height-<?php echo "$number"; ?>" title="Height" style="line-height:35px;">Height: <input type="integer" style="width: 80px;" id="silverlightStreaming-height-<?php echo "$number"; ?>" name="silverlightStreaming-height-<?php echo "$number"; ?>" value="<?php echo htmlspecialchars($options[$number]['height']); ?>" /></label><br />
		<label for="silverlightStreaming-idSLS-<?php echo "$number"; ?>" title="Id Silverlight Streaming" style="line-height:35px;">Id Silverlight Streaming: <input type="integer" style="width: 80px;" id="silverlightStreaming-idSLS-<?php echo "$number"; ?>" name="silverlightStreaming-idSLS-<?php echo "$number"; ?>" value="<?php echo htmlspecialchars($options[$number]['idSLS']); ?>" /></label><br />
		<label for="silverlightStreaming-appName-<?php echo "$number"; ?>" title="Application Name" style="line-height:35px;">Application Name: <input type="integer" style="width: 80px;" id="silverlightStreaming-appName-<?php echo "$number"; ?>" name="silverlightStreaming-appName-<?php echo "$number"; ?>" value="<?php echo htmlspecialchars($options[$number]['appName']); ?>" /></label><br />

	<?php
	}

	function widget_silverlightStreaming($args, $number = 1) {
		$dytwVersion = "Silverlight 1.0";
		extract($args);
		$options = get_option('widget_silverlightStreaming');
		
		$title = $options[$number]['title'];
		$width = $options[$number]['widht'];
		$height = $options[$number]['height'];
		$idSLS = $options[$number]['idSLS'];
		$appName = $options[$number]['appName'];

		$maxitems = 10;
		echo "<div id='Wrapper_SLS". $number ."' style='width:". $width ."x; height:" . $height . "px; overflow:hidden;'></div>";
		echo "<script type='text/javascript'>";
		echo "var Wrapper_SLS = document.getElementById('Wrapper_SLS".$number."');";
		echo "CreateSilverlightSL('" . $idSLS . "','" . $appName . "');";
		echo "</script>";
	}

	function widget_silverlightStreaming_setup() {
		$options = $newoptions = get_option('widget_silverlightStreaming');
		if ( isset($_POST['silverlightStreaming-number-submit']) ) {
			$number = (int) $_POST['silverlightStreaming-number'];
			if ( $number > 9 ) $number = 9;
			if ( $number < 1 ) $number = 1;
			$newoptions['number'] = $number;
		}
		if ( $options != $newoptions ) {
			$options = $newoptions;
			update_option('widget_silverlightStreaming', $options);
			widget_silverlightStreaming_register($options['number']);
		}
	}
	
	function widget_silverlightStreaming_page() {
		$options = $newoptions = get_option('widget_silverlightStreaming');
	?>
		<div class="wrap">
			<form method="POST">
				<h2><?php _e("SilverlightStreaming Widgets", "widgets"); ?></h2>
				<p style="line-height: 30px;"><?php _e('How many SilverlightStreaming widgets would you like?', 'widgets'); ?>
				<select id="silverlightStreaming-number" name="silverlightStreaming-number" value="<?php echo $options['number']; ?>">
	<?php for ( $i = 1; $i < 10; ++$i ) echo "<option value='$i' ".($options['number']==$i ? "selected='selected'" : '').">$i</option>"; ?>
				</select>
				<span class="submit"><input type="submit" name="silverlightStreaming-number-submit" id="silverlightStreaming-number-submit" value="<?php _e('Save'); ?>" /></span></p>
			</form>
		</div>
	<?php

	} 
	
	function widget_silverlightStreaming_head() {
   			$dytwpath = get_option('siteurl')."/wp-content/plugins/silverlightStreaming-widget/";
			echo("<!-- This loads the required scripts for SilverlightStreaming Widget -->");
			echo("<script type='text/javascript' src='http://agappdom.net/h/silverlight.js'></script>");
		    echo("<script type='text/javascript' src='".$dytwpath."/js/CreateSilverlight.js'></script>");
			echo("<!-- End of script load SilverlightStreaming Widget -->");
	}
	
	function widget_silverlightStreaming_footer(){
			echo("<!-- End of container for the SilverlightStreaming Widget Widget -->");
	}
	
	
	function widget_silverlightStreaming_register() {
		$options = get_option('widget_silverlightStreaming');
		$number = $options['number'];
		if ( $number < 1 ) $number = 1;
		if ( $number > 9 ) $number = 9;
		for ($i = 1; $i <= 9; $i++) {
			$name = array('SilverlightStreaming Widget %s', 'widgets', $i);
			register_sidebar_widget($name, $i <= $number ? 'widget_silverlightStreaming' : /* unregister */ '', $i);
			register_widget_control($name, $i <= $number ? 'widget_silverlightStreaming_control' : /* unregister */ '', 490, 495, $i);
		}
		add_action('sidebar_admin_setup', 'widget_silverlightStreaming_setup');
		add_action('sidebar_admin_page', 'widget_silverlightStreaming_page');
		if ( is_active_widget('widget_silverlightStreaming') ) {
			add_action('wp_head', 'widget_silverlightStreaming_head'); 
			add_action('wp_footer', 'widget_silverlightStreaming_footer');
		}

	}
	
	
	add_action('init', 'widget_silverlightStreaming_register', 5);
    
}
add_action('plugins_loaded', 'widget_silverlightStreaming_init'); 
?>